package com.lvy.hczn.transmit.controller;

import com.alibaba.fastjson.JSON;
import com.lvy.hczn.front.common.core.domain.AjaxResult;
import com.lvy.hczn.front.common.exception.UtilException;
import com.lvy.hczn.mq.rocket.ParamInfo;
import com.lvy.hczn.transmit.domain.dto.cteck.Container;
import com.lvy.hczn.transmit.domain.dto.cteck.GoodsBag;
import com.lvy.hczn.transmit.service.ContainerService;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import java.util.ArrayList;
import java.util.List;

@RequestMapping("/api/dt/container/")
@RestController
@Slf4j
public class ContainerController {

    @Autowired
    private ContainerService containerService;

    @ApiOperation(value = "批量新增货箱到平台", notes = "批量新增货箱到平台")
    @PostMapping("batch")
    public AjaxResult syncContainerToPlatform(HttpServletRequest request, @RequestBody ParamInfo info) {
        try {
            if (info == null || info.getList() == null || info.getList().isEmpty()) {
                throw new UtilException("参数不能为空");
            }
            List<Container> list = JSON.parseArray(JSON.toJSONString(info.getList()), Container.class);
            containerService.syncContainerToPlatForm(list);
            return AjaxResult.success();
        } catch (Exception e) {
            if (e instanceof UtilException) {
                return AjaxResult.error(e.getMessage());
            } else {
                log.error("syncContainerToPlatform同步异常：{}", e);
                return AjaxResult.error("系统异常");
            }
        }
    }
}
